package com.qhx.rtkcloud.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TcpServerMock11011 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(11011);
        Socket socket =  serverSocket.accept();
        Executors.newScheduledThreadPool(1)
                .scheduleAtFixedRate(() -> {
                    try {
                        socket.getOutputStream().write((11011 + "send msg" + new Date().toString()).getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }, 1,1, TimeUnit.SECONDS);

        new Thread(() -> {
            InputStream inputStream = null;
            try {
                inputStream = socket.getInputStream();
                byte[] bytes = new byte[10240];
                int length = 0;
                while((length = inputStream.read(bytes)) > 0) {
                    System.out.println("11011 receive:" + new String(Arrays.copyOfRange(bytes, 0, length)));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }).start();

    }
}
